#include "stdafx.h"
#include "linked_list.h"
#include <stdlib.h>
#include "fetal.h"
//return true if the list is empty

//error helper function I still don't know how so resove the Fetal Error;
void FetalError(char *C){
	printf(C);
}

int  IsEmpty(List L){
	return L->Next == NULL;
}

//return true if P is the last position of L
int IsLast(Position P,List L){
	if(P == NULL){
		
	}
	return P->Next == NULL;
}

//return the position of X in L, NULL if not found
Position Find(ElementType X, List L){
	Position P;

	P = L->Next;
	while(P != NULL && P->Element != X){
		P = P->Next;
	}

	return P;

}

//delete first occurrence of X from a list
//asume use of a header node

void Delete(ElementType X,List L){
	Position P,TmpCell;

	P = FindPrevious(X,L);

	if(!IsLast(P,L)){
		TmpCell = P->Next;
		P->Next = TmpCell->Next;
		free(TmpCell);
	}

}
//if X is not found, then Next field of returned Position is NULL 
//assumes a header
Position FindPrevious(ElementType X, List L){
	Position P;

	P = L;
	while(P->Next != NULL && P->Element != X)
		P = P->Next;
	
	return P;

}

//Insert(after a legal position P)
//Header implementation assumed
//Parameter L is unused in this implementation

void Insert(ElementType X,List L,Position P){
	Position TmpCell;

	TmpCell = (Position)malloc(sizeof(struct Node));

	if(TmpCell == NULL){
		FetalError("Out of space!!!");
	}
	TmpCell->Element = X;
	TmpCell->Next = P->Next;
	P->Next = TmpCell;

}


//Delete a List
//Header assumed
void DeleteList(List L){
	Position P,Tmp;

	P = L->Next;
	L->Next = NULL;
	while(P != NULL){
		Tmp = P->Next;
		free(P);
		P = Tmp;
	}

}
//return the header of the linked list
Position Header(List L){
	return L;
}

//Return the first element's Position of the linked list
Position First(List L){
	return L->Next;
}
//return the next position
Position Advance(Position P){
	return P->Next;
}
//return the element at position p
ElementType Retrive(Position P){
	return P->Element;
}